package com.ice.base.service.impl;

import com.ice.base.constant.BaseConstant;
import com.ice.base.dao.SysMenuDao;
import com.ice.base.model.*;
import com.ice.base.service.*;
import com.ice.base.dao.BaseDao;
import com.ice.base.util.BaseUtil;
import com.ice.base.util.PinYinHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Date: 2018-12-24
 * Time: 09:03
 */
@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu> implements SysMenuService {
    private Logger logger = LogManager.getLogger(getClass());

    @Resource
    private SysMenuDao dao;

    @Resource
    private SysPermissionService sysPermissionService;
    @Resource
    private SysRolePermissionService sysRolePermissionService;

    @Override
    public BaseDao<SysMenu> getBaseDao() {
        return dao;
    }

    @Override
    public Integer insertRBAC(SysMenu sysMenuModel) {
        //添加菜单
        Integer result = insert(sysMenuModel);
        if (result != 0) {
            //添加菜单对应的权限
            SysPermissionModel sysPermissionModel = new SysPermissionModel();
            sysPermissionModel.init(sysPermissionModel);
            sysPermissionModel.setId(BaseUtil.getUUID());
            sysPermissionModel.setPermission_name(sysMenuModel.getMenu_name());
            sysPermissionModel.setPermission_code(BaseConstant.PERMISSION_MENU_PRE + PinYinHelper.getFirstSpell(sysMenuModel.getMenu_name()).toUpperCase());
            sysPermissionModel.setPermission_band_menu_id(sysMenuModel.getId());
            sysPermissionModel.setPermission_type(BaseConstant.PERMISSION_TYPE_ENUM.TYPE_MENU.getCode());
            sysPermissionService.insert(sysPermissionModel);
        }
        return result;
    }

    /**
     * @param id  菜单的id
     * @return
     */
    @Override
    @Transactional
    public Integer deleteRBAC(String id) {
        //删除菜单
        Integer result = delete(id);
        if (result != 0) {
            //删除对应权限
            sysPermissionService.deleteByMenuId(id);//此处自动删除权限对应的角色关系
        }
        return result;
    }

    @Override
    public Integer updateRBAC(SysMenu sysMenuModel) {
        Integer result = update(sysMenuModel);
        if (result != 0) {
            SysPermissionModel sysPermissionModel = new SysPermissionModel();
            sysPermissionModel.setPermission_name(sysMenuModel.getMenu_name());
            sysPermissionModel.setPermission_code(BaseConstant.PERMISSION_MENU_PRE + PinYinHelper.getFirstSpell(sysMenuModel.getMenu_name()).toUpperCase());
            sysPermissionModel.setPermission_band_menu_id(sysMenuModel.getId());
            sysPermissionService.updateByMenuId(sysPermissionModel);
        }
        return result;
    }

    @Override
    public List<SysMenu> selectRBAC() {
        ArrayList<SysMenu> resultList = new ArrayList<>();
        ArrayList<String> idList = new ArrayList<>();
         SysLoginModel currLoginModel = BaseUtil.getCurrLoginModel();
        try{
            if (currLoginModel != null) {
                List<SysRoleModel> roles = currLoginModel.getRoles();
                if (roles != null) {
                    for (SysRoleModel role : roles) {
                        List<SysPermissionModel> permissions = role.getPermissions();
                        if (permissions != null) {
                            int count = 0;
                            for (SysPermissionModel permission : permissions) {
                               if(null == permission){
                                    System.out.println(currLoginModel.getRealName()+",空权限个数："+count++);
                                    continue;
                                }
                              /*   if(null != permission.getId() && permission.getId().equals("a09f35fd18bc4ff2bb37b7bb910ee056")){
                                    System.out.println(111);
                                }*/
                                String menuId = permission.getPermission_band_menu_id();
                                SysMenu sysMenuModel = selectById(menuId);
                                if (sysMenuModel != null && ! idList.contains(sysMenuModel.getId())) {
                                    idList.add(sysMenuModel.getId());
                                    resultList.add(sysMenuModel);
                                }
                            }
                            //Collections.sort(resultList, Collections.reverseOrder());
                        }
                    }
                }

            } else {
                logger.error("用户登录信息异常");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultList;
    }
}
